import java.util.Scanner;

public class CinemaSystem {
    private final Film[] films = new Film[10];
    private final Session[] sessions = new Session[20];
    private final User[] users = new User[10];
    private final Scanner scanner = new Scanner(System.in);

    public CinemaSystem() {

        films[0] = new Film("流浪地球", "郭帆", "吴京", "科幻冒险", "170分钟", 9.0);
        films[1] = new Film("满江红", "张艺谋", "沈腾", "悬疑剧情", "150分钟", 8.5);

        sessions[0] = new Session("S001", "流浪地球", "2025-01-01 10:00", "黄金厅", "50", 8, 10);
        sessions[1] = new Session("S002", "满江红", "2025-01-01 14:00", "白银厅", "45", 10, 12);

        users[0] = new User("admin", "管理员", "admin123", "2025-01-01", "管理员", "12345678901");
        users[1] = new User("manager", "李经理", "manager123", "2025-01-02", "经理", "12345678902");
        users[2] = new User("reception", "王前台", "reception123", "2025-01-03", "前台", "12345678903");
    }

    public void start() {
        System.out.println("=== 万东电影院管理系统 ===");
        while (true) {
            System.out.println("请选择登录身份：1-管理员 2-经理 3-前台 4-退出系统");
            int choice = scanner.nextInt();
            scanner.nextLine();
            switch (choice) {
                case 1:
                    adminLogin();
                    break;
                case 2:
                    managerLogin();
                    break;
                case 3:
                    receptionistLogin();
                    break;
                case 4:
                    System.out.println("退出系统，欢迎下次光临！");
                    return;
                default:
                    System.out.println("输入有误，请重新选择！");
            }
        }
    }

    private void adminLogin() {
        System.out.println("\n=== 管理员登录 ===");
        System.out.println("请输入用户名：");
        String id = scanner.nextLine();
        System.out.println("请输入密码：");
        String pwd = scanner.nextLine();

        for (User user : users) {
            if (user != null && "管理员".equals(user.getType())
                    && user.getId().equals(id) && user.getPassword().equals(pwd)) {
                System.out.println("管理员登录成功！");
                adminMenu(user);
                return;
            }
        }
        System.out.println("用户名或密码错误，登录失败！\n");
    }

    private void adminMenu(User admin) {
        while (true) {
            System.out.println("\n=== 管理员菜单 ===");
            System.out.println("1-密码管理 2-用户管理 3-退出登录");
            int choice = scanner.nextInt();
            scanner.nextLine();
            switch (choice) {
                case 1:
                    manageAdminPassword(admin);
                    break;
                case 2:
                    manageUsers();
                    break;
                case 3:
                    System.out.println("退出管理员登录！\n");
                    return;
                default:
                    System.out.println("输入有误，请重新选择！");
            }
        }
    }

    private void manageAdminPassword(User admin) {
        System.out.println("\n=== 密码管理 ===");
        System.out.println("1-修改自身密码 2-重置非管理员密码 3-返回");
        int choice = scanner.nextInt();
        scanner.nextLine();
        switch (choice) {
            case 1:
                System.out.println("请输入原密码：");
                String oldPwd = scanner.nextLine();
                if (oldPwd.equals(admin.getPassword())) {
                    System.out.println("请输入新密码：");
                    String newPwd = scanner.nextLine();
                    admin.setPassword(newPwd);
                    System.out.println("自身密码修改成功！");
                } else {
                    System.out.println("原密码错误，修改失败！");
                }
                break;
            case 2:
                System.out.println("请输入要重置密码的用户名：");
                String userId = scanner.nextLine();
                for (User user : users) {
                    if (user != null && !"管理员".equals(user.getType()) && user.getId().equals(userId)) {
                        user.setPassword("123456");
                        System.out.println("用户 " + userId + " 密码重置成功，默认密码：123456");
                        return;
                    }
                }
                System.out.println("用户不存在，重置失败！");
                break;
            case 3:
                return;
            default:
                System.out.println("输入有误，请重新选择！");
        }
    }

    private void manageUsers() {
        while (true) {
            System.out.println("\n=== 用户管理 ===");
            System.out.println("1-列出用户 2-添加用户 3-删除用户 4-查询用户 5-返回");
            int choice = scanner.nextInt();
            scanner.nextLine();
            switch (choice) {
                case 1:
                    listUsers();
                    break;
                case 2:
                    addUser();
                    break;
                case 3:
                    deleteUser();
                    break;
                case 4:
                    queryUser();
                    break;
                case 5:
                    return;
                default:
                    System.out.println("输入有误，请重新选择！");
            }
        }
    }

    private void listUsers() {
        System.out.println("\n=== 用户列表 ===");
        System.out.println("ID\t姓名\t类型\t注册时间\t手机号");
        for (User user : users) {
            if (user != null) {
                System.out.printf("%s\t%s\t%s\t%s\t%s%n",
                        user.getId(), user.getName(), user.getType(),
                        user.getRegisterTime(), user.getPhone());
            }
        }
    }

    private void addUser() {
        System.out.println("\n请输入用户ID：");
        String id = scanner.nextLine();
        System.out.println("请输入用户名：");
        String name = scanner.nextLine();
        System.out.println("请输入密码：");
        String pwd = scanner.nextLine();
        System.out.println("请输入注册时间（如2025-01-01）：");
        String regTime = scanner.nextLine();
        System.out.println("请输入用户类型（经理/前台）：");
        String type = scanner.nextLine();
        System.out.println("请输入手机号：");
        String phone = scanner.nextLine();

        for (int i = 0; i < users.length; i++) {
            if (users[i] == null) {
                users[i] = new User(id, name, pwd, regTime, type, phone);
                System.out.println("用户添加成功！");
                return;
            }
        }
        System.out.println("用户数量已达上限，添加失败！");
    }

    private void deleteUser() {
        System.out.println("\n请输入要删除的用户ID（admin不可删除）：");
        String delId = scanner.nextLine();
        if ("admin".equals(delId)) {
            System.out.println("管理员账号不可删除！");
            return;
        }

        for (int i = 0; i < users.length; i++) {
            if (users[i] != null && users[i].getId().equals(delId)) {
                users[i] = null;
                System.out.println("用户删除成功！");
                return;
            }
        }
        System.out.println("用户不存在，删除失败！");
    }

    private void queryUser() {
        System.out.println("\n请输入要查询的用户ID或用户名：");
        String queryStr = scanner.nextLine();
        boolean found = false;

        System.out.println("\n=== 查询结果 ===");
        System.out.println("ID\t姓名\t类型\t注册时间\t手机号");
        for (User user : users) {
            if (user != null && (user.getId().equals(queryStr) || user.getName().equals(queryStr))) {
                System.out.printf("%s\t%s\t%s\t%s\t%s%n",
                        user.getId(), user.getName(), user.getType(),
                        user.getRegisterTime(), user.getPhone());
                found = true;
            }
        }

        if (!found) {
            System.out.println("未找到匹配用户！");
        }
    }

    private void managerLogin() {
        System.out.println("\n=== 经理登录 ===");
        System.out.println("请输入用户名：");
        String id = scanner.nextLine();
        System.out.println("请输入密码：");
        String pwd = scanner.nextLine();

        for (User user : users) {
            if (user != null && "经理".equals(user.getType())
                    && user.getId().equals(id) && user.getPassword().equals(pwd)) {
                System.out.println("经理登录成功！");
                Manager manager = new Manager(films, sessions, users);
                managerMenu(manager);
                return;
            }
        }
        System.out.println("用户名或密码错误，登录失败！\n");
    }

    private void managerMenu(Manager manager) {
        while (true) {
            System.out.println("\n=== 经理菜单 ===");
            System.out.println("1-影片管理 2-排片管理 3-退出登录");
            int choice = scanner.nextInt();
            scanner.nextLine();
            switch (choice) {
                case 1:
                    manager.manageFilms();
                    break;
                case 2:
                    manager.manageSessions();
                    break;
                case 3:
                    System.out.println("退出经理登录！\n");
                    return;
                default:
                    System.out.println("输入有误，请重新选择！");
            }
        }
    }

    private void receptionistLogin() {
        System.out.println("\n=== 前台登录 ===");
        System.out.println("请输入用户名：");
        String id = scanner.nextLine();
        System.out.println("请输入密码：");
        String pwd = scanner.nextLine();

        for (User user : users) {
            if (user != null && "前台".equals(user.getType())
                    && user.getId().equals(id) && user.getPassword().equals(pwd)) {
                System.out.println("前台登录成功！");
                Receptionist receptionist = new Receptionist(films, sessions);
                receptionist.work();
                System.out.println("退出前台登录！\n");
                return;
            }
        }
        System.out.println("用户名或密码错误，登录失败！\n");
    }
}